/**
 * @author Mie Plougstrup, Anders Abildgaard, Bo Stokholm 
 * @Version 16-12-2014
 */
package ctrLayer;

import java.util.ArrayList;

import modelLayer.Address;
import modelLayer.BusinessPartner;
import modelLayer.Customer;
import modelLayer.Supplier;
import dbLayer.DbBusinessPartner;
import dbLayer.DbConnection;
import dbLayer.DbMaxId;

public class CtrBusinessPartner {

	private DbBusinessPartner dbBusinessPartner;
	private CtrAddress ctrAddress;
	private DbConnection con;

	/**
	 * Constructor
	 */
	public CtrBusinessPartner(){
		dbBusinessPartner = new DbBusinessPartner();
		ctrAddress = new CtrAddress();
		con = DbConnection.getInstance();
	}
	/**
	 * Create a customer and insert it into a database.
	 * @param String businessName
	 * @param String phoneNo
	 * @param ArrayList<Address> aList
	 * @return Customer object
	 * @throws Exception
	 */
	public Customer createCustomer(String businessName, String phoneNo, ArrayList<Address> aList) throws Exception {
		try {
			con.startTransaction();
			for(Address aObj : aList) {
				ctrAddress.insertAddress(aObj);
			}
			Customer cObj = new Customer(businessName, phoneNo);
			cObj.setAdList(aList);
			int id = DbMaxId.getMaxID("partnerId", "businessPartner") + 1;
			cObj.setPartnerId(id);
			dbBusinessPartner.insertCustomer(cObj);
			for(Address aObj : cObj.getAdList()){
				ctrAddress.updateBpAddress(aObj, cObj.getPartnerId());
			}
			con.commitTransaction();
			return dbBusinessPartner.retrieveCustomerById(id);
		} catch(Exception e) {
			con.rollbackTransaction();
			throw e;
		}
	}
	/**
	 * Create a supplier and insert it into supplier.
	 * @param String businessName
	 * @param String phoneNo
	 * @param Address address
	 * @return Supplier object
	 * @throws Exception
	 */
	public Supplier createSupplier(String businessName, String phoneNo, Address address) throws Exception {
		Supplier sObj = new Supplier(businessName, phoneNo);
		sObj.setAddress(address);
		int id = DbMaxId.getMaxID("partnerId", "businessPartner") + 1;
		sObj.setPartnerId(id);
		dbBusinessPartner.insertSupplier(sObj);
		ctrAddress.updateBpAddress(sObj.getAddress(), sObj.getPartnerId());
		return dbBusinessPartner.retrieveSupplierById(id);
	}
	/**
	 * Retrieve supplier by id from the database.
	 * @param int supplierId
	 * @return Supplier object
	 */
	public Supplier retrieveSupplier(int supplierId) {
		return dbBusinessPartner.retrieveSupplierById(supplierId);
	}
	/**
	 * Retrieve Customer by id from the database.
	 * @param int customerId
	 * @return Customer object
	 */
	public Customer retrieveCustomer(int customerId) {
		return dbBusinessPartner.retrieveCustomerById(customerId);
	}
	/**
	 * Update a customer in the database.
	 * @param Customer cObj
	 */
	public void updateCustomer(Customer cObj) {
		for(Address aObj : cObj.getAdList()){
			ctrAddress.updateBpAddress(aObj, cObj.getPartnerId());
		}
		dbBusinessPartner.updateCustomer(cObj);
	}
	/**
	 * Update a supplier in the database.
	 * @param Supplier sObj
	 */
	public void updateSupplier(Supplier sObj) {
		ctrAddress.updateBpAddress(sObj.getAddress(), sObj.getPartnerId());
		dbBusinessPartner.updateSupplier(sObj);
	}
	/**
	 * Delete a Businesspartner from the database
	 * @param BusinessPartner bpObj
	 */
	public void delete(BusinessPartner bpObj) {
		dbBusinessPartner.delete(bpObj);
	}

	/**
	 * Retrieve a supplier by phone
	 * @param String phone
	 * @return Supplier object
	 */
	public Supplier retrieveByPhone(String phone){
		return dbBusinessPartner.retrieveByPhone(phone);
	}

	/**
	 * Retrieve a customer by phone
	 * @param String phone
	 * @return Customer object
	 */
	public boolean isPhoneUsed(String phone) {
		return dbBusinessPartner.phoneNoUsed(phone);
	}

	public boolean isNameUsed(String name) {
		return dbBusinessPartner.nameUsed(name);
	}

	/**
	 * Retrieves a list of customers by their name.
	 * 
	 * @param name
	 * @return
	 */
	public ArrayList<Customer> retrieveCustomersByName(String name) {
		return dbBusinessPartner.retrieveCustomerByName(name);
	}

	/**
	 * Retrieve a supplier by name
	 * @param String name
	 * @return Supplier object
	 */
	public Supplier retrieveSupplierByName(String name){
		return dbBusinessPartner.retrieveSupplierByName(name);
	}
	public ArrayList<String> retrieveSupNamesByString(String text) {
		return dbBusinessPartner.retrieveSupNameListByString(text);
	}
}